//获取应用实例
const app = getApp();
var userInfo;
wx.cloud.init({
  env: 'cloud1-7gv4qn128a030d31'
})
const db = wx.cloud.database().collection("user")
var that;

Page({
  data: {
    userType:'',
    openid:"",
    items:[
      {value: 'stu', name: '我是学生'},
      {value: 'hr', name: '我是商家'}
    ],
    isSelectId:false,
    mySelect:''
  }, 
// 单项选择
  radioChange(e) {
      that=this
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
    that.setData({
      userType:e.detail.value
    })
  },
  
  onLoad() {
    that = this;
    // console.log(userInfo);
    that.getOpenid();
    const user=wx.getStorageSync('user')
    if(user){
        console.log("存储中有user");
        that.setData({
          isSelectId:true,
          mySelect:user.userType
        })
        console.log(user);
        // 判断页面并跳转
        if(user.userType==="stu"){
          wx.reLaunch({
            url: '../../../pages/me/index/index?nickName='+user.nickName+'&avatarUrl='+user.avatarUrl+'&userType='+user.userType,
          })
        }else{
          wx.reLaunch({
             url: '../../../pages/me/index/index?nickName='+user.nickName+'&avatarUrl='+user.avatarUrl+'&userType='+user.userType,
          })
        }
    }
  },
// 获取用户openid
getOpenid() {
    that = this;
    wx.cloud.callFunction({
     name: 'getOpenid',
     complete: res => {
        //  console.log('-------------',res);
    //   console.log('云函数获取到的openid: ',res.result.openid)
      var _openid = res.result.openid;
      that.setData({
       openid: _openid
      })
     }
    })
   },
  getUserInfo: function (e) {
    that=this;
//    console.log(e);
   if(!that.data.userType){
     wx.showToast({
       title: '选择身份后才可登录',
       icon:'none'
     })
     return
   }
   wx.getUserProfile({
     desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
     success: (res) => {
       console.log("获取用户信息成功", res)
       that.userAdd(res)
     },
     fail: res => {
       console.log("获取用户信息失败", res)
       wx.showModal({
                 title: '警告',
                 content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
                 showCancel: false,
                 confirmText: '返回授权',
             })
     }
 })   
},

  userAdd(res) {
      that=this
    var user = {
      userType: that.data.userType,
      nickName: res.userInfo.nickName,
      avatarUrl: res.userInfo.avatarUrl,
      openid: that.data.openid
    };
    // 加入本地存储
    wx.setStorageSync('user', user);
    console.log(user);
    // 存入globaldata
    app.globalData.nickname=res.userInfo.nickName
    app.globalData.usertype=that.data.userType
    app.globalData. avatarurl=res.userInfo.avatarUrl
    app.globalData.openid=that.data.openid
    that.setData({
      isSelectId:true,
      mySelect:that.data.userType
    })
    // 加入数据库
    db.add({
        data:{
            userType: that.data.userType,
            nickName: res.userInfo.nickName,
            avatarUrl: res.userInfo.avatarUrl,
            openid: that.data.openid
        },
        success(res){
            console.log("user存入成功",res);
        },
        fail(err){
            // console.log("err",err);
        }
        
    })
  },
 
  loginTo(){
    const user=wx.getStorageSync('user');
    console.log(user.userType);
    if(user.userType==="stu"){
      wx.reLaunch({
        url: '../../../pages/me/index/index?nickName='+user.nickName+'&avatarUrl='+user.avatarUrl+'&userType='+user.userType,
      })
    }else{
      wx.reLaunch({
         url: '../../../pages/me/index/index?nickName='+user.nickName+'&avatarUrl='+user.avatarUrl+'&userType='+user.userType,
      })
    }
  }

})